package com.societyGames.flashForms
{
import com.societyGames.system.EventObject;

import flash.events.Event;

/*
 Passes width and height to child. If no size is specified, child pushes outward.
 */
public class DirectContainer extends SingleItemContainer implements IChangeSize
{
  private var _desiredWidth:Number;
  private var _desiredHeight:Number;
  private var _sizeChangedEventObject:EventObject;

  public function DirectContainer()
  {
    _sizeChangedEventObject = new EventObject(this, "SIZE_CHANGED");
  }

  public function get sizeChangedEventObject():EventObject
  {
    return _sizeChangedEventObject;
  }

  public override function set width(value:Number):void
  {
    _desiredWidth = value;
    recalculate();
  }

  public override function set height(value:Number):void
  {
    _desiredHeight = value;
    recalculate();
  }

  /**
   * What the container should do if the child size changes.
   **/
  override protected function childSizeChangedHandler(event:Event):void
  {
    if (_item && (isNaN(_desiredWidth) || isNaN(_desiredHeight))) //If people outside aren't telling us how big to be, just push the boundary out.
    {
      _sizeChangedEventObject.dispatchEvent();
    }
    else
    {
      recalculate();
    }
  }

  private function recalculate():void
  {
    if (this._item != null)
    {
      this._item.width = this._desiredWidth;
      this._item.height = this._desiredHeight;
    }
  }
}
}